import 'package:flutter/material.dart';

import '../../../bean/dropswitch_bean.dart';
import '../../../common/data_source.dart';
import '../../../common/system_config.dart';
import '../../../utils/tools/analysis_code.dart';
import '../../../utils/tools/screenutil_tool.dart';

class ListWidget extends StatelessWidget {
  List<Widget> lw = [];
  ListWidget(BuildContext context, List<DropSwitchBean> data){
    lw = createMetaList(context, data);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          width: double.infinity,
          margin: EdgeInsets.only(top: ST.seth(5)),
          child: Column(
            children: [
              Container(
                width: double.infinity,
                margin: EdgeInsets.only(top: ST.seth(5)),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: lw,
                ),
              )
            ],
          ),
        )
      ],
    );
  }

  /// ### 创建设备列表
  /// #### [context] BuildContext|必填|上下文
  /// #### [data] List<WirelesstmBean>|必填|返回的数据
  List<Widget> createMetaList(BuildContext context, List<DropSwitchBean> data) {
    TextStyle textStyle = TextStyle(fontSize: SystemConfig.fs_title3);
    List<Widget> _insideListWidget = [];

    if(data != null) {
      data.forEach((value) {
        _insideListWidget.add(
          Container(
            width: double.infinity,
            margin: EdgeInsets.only(bottom: ST.seth(15)),
            decoration: BoxDecoration(
              color: Theme.of(context).backgroundColor,
              borderRadius: BorderRadius.all(Radius.circular(8)),
              boxShadow: [
                BoxShadow(
                  offset: Offset(0, 2), //x,y轴
                  color: Color.fromRGBO(0, 0, 0, .1),
                  blurRadius: 3 //投影距离
                )
              ]
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding:EdgeInsets.symmetric(vertical: ST.seth(20),horizontal: ST.seth(30)),
                  child: Text("${value.name}", style: textStyle)
                ),
                ...createItemList(value.tempList)
              ],
            ),
          ),
        );
      });
    }
    // 返回数据
    return _insideListWidget;
  }


  /// ### 创建数据列表
  /// #### [data] List|必填|对应设备的数据
  List<Widget> createItemList(List data) {
    TextStyle textStyle = TextStyle(fontSize: SystemConfig.fs_content3);
    List<Widget> lw = [];
    if(data!=null){
      for(int i = 0; i < data.length; i++){
        lw.add(
          Container(
            decoration: BoxDecoration(
              border: Border(top: BorderSide(color: SystemConfig.borderColor))
            ),
            child: Row(
              children: [
                Padding(
                  padding:EdgeInsets.symmetric(vertical: ST.seth(15)),
                  child: Row(
                    children: [
                      Container(
                        width: ST.setw(225),
                        alignment: Alignment.center,
                        child: Text("${data[i].createTime}",
                          textAlign: TextAlign.center,
                          style: textStyle
                        )
                      ),
                      Container(
                        width: ST.setw(100),
                        alignment: Alignment.center,
                        child: Text("${data[i].itemName}",
                          textAlign: TextAlign.center,
                          style: textStyle
                        )
                      ),
                      Container(
                        width: ST.setw(125),
                        alignment: Alignment.center,
                        child: Text("${data[i].degree == null || data[i].degree == "" ? '-' : data[i].degree }",
                          textAlign: TextAlign.center,
                          style: textStyle
                        )
                      ),
                      Container(
                        width: ST.setw(165),
                        alignment: Alignment.center,
                        child: Text("${data[i].i == null || data[i].i == "" ? '-' : data[i].i }",
                          textAlign: TextAlign.center,
                          style: textStyle
                        )
                      ),
                      Container(
                        width: ST.setw(130),
                        alignment: Alignment.center,
                        child: Text("${analysisCode(data[i].status, equipmentStateSourceData)["value"]}",
                          style: TextStyle(
                            fontSize: SystemConfig.fs_content3,
                            color: data[i].status != 0 ? Colors.red : Colors.green
                          ),
                          textAlign: TextAlign.center
                        )
                      ),
                    ],
                  ),
                ),
              ],
            ),
          )
        );
      }
    }

    // 返回
    return lw;
  }


}